// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-05 15:11:47
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:14:25
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Material/MAT4Parser.cpp
 */

#include "Import/BDFImport/BulkData/Material/MAT4Parser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Material/IThermalMaterialService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (MAT4);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/MAT4/MAT4.md>
*/
// 以下需要修改
enum MAT4key : std::uint8_t
{
    MAT4KEY_MID     = 2,
    MAT4KEY_K       = 3,   ///< 热导率
    MAT4KEY_CP      = 4,   ///< 比热容
    MAT4KEY_RHO     = 5,   ///< 密度
    MAT4KEY_H       = 6,   ///< 自由对流换热系数
    MAT4KEY_MU      = 7,   ///< 动力粘度
    MAT4KEY_HGEN    = 8,   ///< 用于QVOL条目的热生成能力
    MAT4KEY_REFENTH = 9,   ///< 参考焓
    MAT4KEY_TCH     = 12,  ///< 相变区域发生的最低温度限制
    MAT4KEY_TDELTA  = 13,  ///< 相变发生的总温度变化范围
    MAT4KEY_QLAT    = 14,  ///< 相变相关的单位质量的潜热

};

Common::Status BDF::BULKDATA::ParseMAT4 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                         std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存MAT4材料数据的临时结构体对象
    FEM::ThermalMaterial tempMAT4;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempMAT4.m_DOMAIN_ID = 1;

    // 初始化当前处理的字符串条目的ID
    int curIDKey = 1;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
                // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前条目为空，则记录对应的缺失项
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        switch (curIDKey)
        {
        case MAT4KEY_MID:
            tempMAT4.m_id = convertToI64 (curStrKey);
            break;
        case MAT4KEY_K:
            tempMAT4.m_K = convertToReal (curStrKey);
            break;
        case MAT4KEY_CP:
            tempMAT4.m_CP = convertToReal (curStrKey);
            break;
        case MAT4KEY_RHO:
            tempMAT4.m_RHO = convertToReal (curStrKey);
            break;
        case MAT4KEY_H:
            tempMAT4.m_H = convertToReal (curStrKey);
            break;
        case MAT4KEY_MU:
            tempMAT4.m_MU = convertToReal (curStrKey);
            break;
        case MAT4KEY_HGEN:
            tempMAT4.m_HGEN = convertToReal (curStrKey);
            break;
        case MAT4KEY_REFENTH:
            tempMAT4.m_REFENTH = convertToReal (curStrKey);
            break;
        case MAT4KEY_TCH:
            tempMAT4.m_TCH = convertToReal (curStrKey);
            break;
        case MAT4KEY_TDELTA:
            tempMAT4.m_TDELTA = convertToReal (curStrKey);
            break;
        case MAT4KEY_QLAT:
            tempMAT4.m_QLAT = convertToReal (curStrKey);
            break;
        default:
            break;
        }

        curIDKey++;
    }

    // 将解析后的MAT4数据通过数据库服务工厂的ThermalMaterial服务添加到数据库中
    return dbServiceFactory->get<IThermalMaterialService> ()->Add (tempMAT4);
}